package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dao.CorePlaceMapper;
import com.corechina.apex_osswords.domain.dao.CoreServiceObjectMapper;
import com.corechina.apex_osswords.domain.entity.CoreServiceObject;
import com.corechina.apex_osswords.domain.vo.CoreServiceObjectVo;
import com.corechina.apex_osswords.service.ICoreServiceObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务对象表 服务实现类
 * </p>
 *
 * @author fyh
 * @since 2023-02-15
 */
@Service
public class CoreServiceObjectServiceImpl extends ServiceImpl<CoreServiceObjectMapper, CoreServiceObject> implements ICoreServiceObjectService {

    @Autowired
    private CoreServiceObjectMapper coreServiceObjectMapper;

    @Autowired
    private CorePlaceMapper corePlaceMapper;

    @Override
    public Response update(CoreServiceObject coreServiceObject) {
        coreServiceObject.setEnterpriseId(null);
        coreServiceObject.setEnterpriseId(ShiroUtils.getEnterpriseId());
        if (coreServiceObjectMapper.updateById(coreServiceObject) > 0) {
            if (coreServiceObject.equals(1)) {
                corePlaceMapper.updateIsDelByServiceId(1, coreServiceObject.getId());
            }
            return Response.ok();
        }
        return Response.error();
    }

    @Override
    public Response updateIsDel(Integer isDel, Integer id) {
        coreServiceObjectMapper.updateIsDel(isDel, id);
        if (isDel.equals(1)) {
            corePlaceMapper.updateIsDelByServiceId(1, id);
        }
        return Response.ok();
    }

    @Override
    public Response getList(Integer isDel, String name, Integer pageIndex, Integer pageSize) {
        IPage<CoreServiceObjectVo> serviceObjectVoIPage = coreServiceObjectMapper.getList(new Page(pageIndex, pageSize), isDel, ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId(), name);
        return Response.ok(serviceObjectVoIPage.getRecords(), serviceObjectVoIPage.getTotal());
    }

}
